/*
 * Created on Oct 3, 2005
 *
 */
package littlecyberwar.control;

import java.awt.Dimension;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;

import littlecyberwar.ui.Element;
import littlecyberwar.ui.MainFrame;
import littlecyberwar.ui.MiniMapPanel;
import littlecyberwar.ui.PlayingBoardPanel;
import littlecyberwar.ui.ScalingDelegate;
import littlecyberwar.util.ElementIdDictionary;
import littlecyberwar.util.Util;
import littlecyberwar.tool.*;

public class LayerControl {

	public LayerControl() {
		super();
		// TODO Auto-generated constructor stub
	}
	public static JMenu getInstance() {
		JMenu menu = new JMenu("Layer");
		
		JMenuItem cb5MenuItem= new JCheckBoxMenuItem ("5-Top");
		cb5MenuItem.setSelected(false);
		menu.add(cb5MenuItem);

		JMenuItem cb4MenuItem= new JCheckBoxMenuItem ("4");
		cb4MenuItem.setSelected(false);
		menu.add(cb4MenuItem);

		JMenuItem cb3MenuItem= new JCheckBoxMenuItem ("3-Default");
		cb3MenuItem.setSelected(true);
		menu.add(cb3MenuItem);

		JMenuItem cb2MenuItem= new JCheckBoxMenuItem ("2");
		cb2MenuItem.setSelected(false);
		menu.add(cb2MenuItem);

		JMenuItem cb1MenuItem= new JCheckBoxMenuItem ("1-Bottom");
		cb1MenuItem.setSelected(false);
		menu.add(cb1MenuItem);


		cb1MenuItem.addItemListener(new ItemListener ()  {
			  public void itemStateChanged(ItemEvent e)  {
				  if ( e.getStateChange() == ItemEvent.SELECTED ){ 
					  layerChanged(0, true);
				  } else {
					  layerChanged(0, false);
				  }
			  }
			});
		
		cb2MenuItem.addItemListener(new ItemListener ()  {
			  public void itemStateChanged(ItemEvent e)  {
				  if ( e.getStateChange() == ItemEvent.SELECTED ){ 
					  layerChanged(1, true);
				  } else {
					  layerChanged(1, false);
				  }
			  }
			});

		cb3MenuItem.addItemListener(new ItemListener ()  {
			  public void itemStateChanged(ItemEvent e)  {
				  if ( e.getStateChange() == ItemEvent.SELECTED ){ 
					  layerChanged(2, true);
				  } else {
					  layerChanged(2, false);
				  }
			  }
			});

		cb4MenuItem.addItemListener(new ItemListener ()  {
			  public void itemStateChanged(ItemEvent e)  {
				  if ( e.getStateChange() == ItemEvent.SELECTED ){ 
					  layerChanged(3, true);
				  } else {
					  layerChanged(3, false);
				  }
			  }
			});

		cb5MenuItem.addItemListener(new ItemListener ()  {
			  public void itemStateChanged(ItemEvent e)  {
				  if ( e.getStateChange() == ItemEvent.SELECTED ){ 
					  layerChanged(4, true);
				  } else {
					  layerChanged(4, false);
				  }
			  }
			});

		return ( menu );
	}

	static void layerChanged( int layerIndex, boolean enable ){
		if ( enable ){
			LayeredUnitHandler.enableLayer(layerIndex);
		} else {
			LayeredUnitHandler.disbaleLayer(layerIndex);
			adjustSelectedElementsInLayer(layerIndex);
		}
		return;
	}

	/**
	 * Now that the said layer has just been disabled, should 
	 * 1) check currently selected elements if any then
	 * 2) if said element is disabled, then unselect it
	 * 
	 * @param layerIndex
	 */
	static void adjustSelectedElementsInLayer( int layerIndex ){
		// now loop through all the elements, and then put each one of them into the right layer
		ArrayList selectedElements= MultiSelectMouseHandler.getSelectedElements();		
		PlayingBoardPanel pb = PlayingBoardPanel.getInstance();
		MouseHandler mh = pb.getCurrHandler();
		
		for ( int i=selectedElements.size()-1; i >= 0; i-- ){
			Element el = ((SelectedElementContainer)selectedElements.get(i)).element;
			
			int src = ((Integer)el.getNewProps(Util._layer)).intValue();			
			// now check if this element is now in an hidden layer, if so, deselect it.
			if ( src == layerIndex ){
				mh.resetSelectedElement( el );
			}
		}
		pb.modelChanged();
		return;
	}
}
